<?php

/**
 * Armory API
 * 
 * The API class for requesting data from the armory.
 * 
 * @author Oliver Schwarz <oliver.schwarz@gmail.com>
 */
class ArmoryAPI
{

        /**
         * API constants
         * @var string
         */
        const useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.0; de-DE; rv:1.6) Gecko/20040206 Firefox/1.0.1';
        const profile_url = 'http://eu.wowarmory.com/character-sheet.xml?r=%s&n=%s&gn=%s';
        const achievements_url = 'http://eu.wowarmory.com/character-achievements.xml?r=%s&n=%s&gn=%s';
        const talents_url = 'http://eu.wowarmory.com/character-talents.xml?r=%s&n=%s&gn=%s';
        const reputation_url = 'http://eu.wowarmory.com/character-reputation.xml?r=%s&n=%s&gn=%s';
        const guild_url = 'http://eu.wowarmory.com/guild-info.xml?r=%s&cn=%s&gn=%s';

        /**
         * Name of character
         * @var string
         */
        public $sCharacterName;
        
        /**
         * Name of realm
         * @var string
         */
        public $sRealmName;

        /**
         * Name of guild
         * @var string
         */
        public $sGuildName;
        
        /**
         * Armory URL
         * @var string
         */
        private $sArmoryUrl;
        
        /**
         * XML data
         * @var string
         */
        private $sXmlData;
        
        /**
         * Parsed data
         * @var object
         */
        public $oData;

        /**
         * Constructor
         * 
         * @param string $sCharacterName Name of character
         * @param string $sRealmName Name of realm
         * @param string $sGuildName Name of guild
         */
        public function __construct($sCharacterName, $sRealmName, $sGuildName)
        {
                $this->sCharacterName = $sCharacterName;
                $this->sRealmName = $sRealmName;
                $this->sGuildName = $sGuildName;
        }
        
        /**
         * Fetch armory data (XML)
         */
        public function fetchArmoryData()
        {
                /*$c = curl_init();
                curl_setopt($c, CURLOPT_URL, $this->sArmoryUrl);
                curl_setopt($c, CURLOPT_USERAGENT, self::useragent);
                curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
                $this->sXmlData = curl_exec($c);
                curl_close($c);*/
                ini_set('user_agent', self::useragent);
                $this->sXmlData = file_get_contents($this->sArmoryUrl);
        }
        
        /**
         * Fetch character info
         */
        public function fetchProfile()
        {
                $this->sArmoryUrl = sprintf(
                        self::profile_url,
                        urlencode($this->sRealmName),
                        urlencode($this->sCharacterName),
                        urlencode($this->sGuildName)
                        );
                $this->fetchArmoryData();
                return new SimpleXMLElement($this->sXmlData);
        }
        
        /**
         * Fetch guild info
         */
        public function fetchGuild()
        {
                $this->sArmoryUrl = sprintf(
                        self::guild_url,
                        urlencode($this->sRealmName),
                        urlencode($this->sCharacterName),
                        urlencode($this->sGuildName)
                        );
                 $this->fetchArmoryData();
                 return new SimpleXMLElement($this->sXmlData);
        }

        /**
         * Fetch achievements
         */
        public function fetchAchievements()
        {
                $this->sArmoryUrl = sprintf(                        
                        self::achievements_url,
                        urlencode($this->sRealmName),
                        urlencode($this->sCharacterName),
                        urlencode($this->sGuildName)
                        );
                 $this->fetchArmoryData();
                 return new SimpleXMLElement($this->sXmlData);
        }
        
}

?>